﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TextMining;

namespace HierarchicalClustering.LinkageCriteria
{
    public class Max : ILinkage   // maximum linkage criteria
    {
        #region ILinkage Members

        public double Distance(Cluster clusterA, Cluster clusterB, HierarchicalClustering.Metrics.IMetric metric)
        {    // max{ d(a,b }
            double max = -1, temp;

            // the distance between 2 clusters is the distance between 2 of their documents
            // which have the maximum distance

            foreach (Document docA in clusterA.Documents)
            {
                foreach (Document docB in clusterB.Documents)
                {
                    temp = metric.Distance(docA, docB);
                    if (max == -1 || temp > max)
                        max = temp;
                }
            }

            return max;
        }

        public double AddWeight(Cluster clusterA, Cluster clusterB, double distance)
        {   // max{ d(a,b) }
            return distance;
        }

        public double NewDistance(double distanceA, double distanceB)
        {
            // max{ d(a,b) } = max{ max{ d(a,b) } = max{ max{ d(a1,b1) }, max{ d(a2,b2) } }
            return Math.Max ( distanceA, distanceB );
        }

        #endregion
    }
}
